package com.example.testplatform.service.impl;

import com.example.testplatform.common.LogUtils;
import com.example.testplatform.entity.Menu;
import com.example.testplatform.mapper.MenuMapper;
import com.example.testplatform.service.MenuService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    
    private static final Logger logger = LogUtils.getLogger(MenuServiceImpl.class);
    
    @Autowired
    private MenuMapper menuMapper;
    
    @Override
    public List<Menu> getMenuList() {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENU_LIST_QUERY_START", "system", "准备获取菜单列表数据");
        try {
            LogUtils.debug(logger, () -> "【菜单管理】开始执行getMenuList方法获取菜单列表");
            LogUtils.debug(logger, () -> "【菜单管理】getMenuList方法执行开始时间戳: " + startTime);
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.findAll方法查询数据库");
            List<Menu> menus = menuMapper.findAll();
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库查询完成，开始处理查询结果");
            LogUtils.info(logger, () -> "【菜单管理】获取菜单列表成功，共 " + menus.size() + " 条记录");
            LogUtils.logBusiness(logger, "MENU_LIST_QUERY_SUCCESS", "system", "获取菜单列表成功，记录数: " + menus.size());
            
            LogUtils.logPerformance(logger, "getMenuList", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】getMenuList方法执行完成，返回菜单列表数据");
            return menus;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】获取菜单列表异常: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENU_LIST_QUERY_FAILED", "system", "获取菜单列表失败: " + e.getMessage());
            throw e;
        }
    }
    
    @Override
    public List<Menu> getAllMenus() {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "ALL_MENUS_QUERY_START", "system", "准备获取所有菜单数据");
        try {
            LogUtils.debug(logger, () -> "【菜单管理】开始执行getAllMenus方法获取所有菜单");
            LogUtils.debug(logger, () -> "【菜单管理】方法执行开始时间戳: " + startTime);
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.findAll方法查询所有菜单数据");
            List<Menu> menus = menuMapper.findAll();
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库查询完成，开始处理查询结果");
            LogUtils.info(logger, () -> "【菜单管理】获取所有菜单成功，共 " + menus.size() + " 条记录");
            LogUtils.logBusiness(logger, "ALL_MENUS_QUERY_SUCCESS", "system", "获取所有菜单成功，记录数: " + menus.size());
            
            LogUtils.logPerformance(logger, "getAllMenus", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】getAllMenus方法执行完成，返回所有菜单数据");
            return menus;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】获取所有菜单异常: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "ALL_MENUS_QUERY_FAILED", "system", "获取所有菜单失败: " + e.getMessage());
            throw e;
        }
    }
    
    @Override
    public Menu getMenuById(Long id) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENU_BY_ID_QUERY_START", "system", "准备根据ID查询菜单，ID: " + id);
        try {
            // 参数验证
            if (id == null || id <= 0) {
                LogUtils.warn(logger, "【菜单管理】getMenuById方法参数验证失败: ID为空或非法值: " + id);
                LogUtils.logBusiness(logger, "MENU_BY_ID_QUERY_FAILED", "system", "根据ID查询菜单参数验证失败: " + id);
                return null;
            }
            
            LogUtils.debug(logger, () -> "【菜单管理】开始执行getMenuById方法，ID: " + id);
            LogUtils.debug(logger, () -> "【菜单管理】getMenuById方法执行开始时间戳: " + startTime);
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.findById方法查询数据库，ID: " + id);
            Menu menu = menuMapper.findById(id);
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库查询完成，开始处理查询结果");
            if (menu == null) {
                LogUtils.debug(logger, () -> "【菜单管理】未找到ID为 " + id + " 的菜单");
                LogUtils.logBusiness(logger, "MENU_BY_ID_NOT_FOUND", "system", "未找到ID为 " + id + " 的菜单");
            } else {
                LogUtils.debug(logger, () -> "【菜单管理】找到菜单: " + menu.getTitle() + " (ID: " + menu.getId() + "), 路径: " + menu.getPath());
                LogUtils.logBusiness(logger, "MENU_BY_ID_FOUND", "system", "找到菜单: " + menu.getTitle() + " (ID: " + menu.getId() + ")");
            }
            
            LogUtils.logPerformance(logger, "getMenuById", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】getMenuById方法执行完成，返回查询结果");
            return menu;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】根据ID查询菜单异常: " + id + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENU_BY_ID_QUERY_ERROR", "system", "根据ID查询菜单发生异常: " + id + ", " + e.getMessage());
            throw e;
        }
    }
    
    @Override
    public List<Menu> getMenusByParentId(Long parentId) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENUS_BY_PARENT_ID_QUERY_START", "system", "准备根据父菜单ID查询子菜单，父ID: " + parentId);
        try {
            // 参数处理
            final Long finalParentId = parentId == null ? 0L : parentId;
            if (parentId == null) {
                LogUtils.debug(logger, () -> "【菜单管理】父菜单ID为空，使用默认值0");
            }
            
            LogUtils.debug(logger, () -> "【菜单管理】开始执行getMenusByParentId方法，父ID: " + finalParentId);
            LogUtils.debug(logger, () -> "【菜单管理】getMenusByParentId方法执行开始时间戳: " + startTime);
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.findByParentId方法查询数据库，父ID: " + finalParentId);
            List<Menu> menus = menuMapper.findByParentId(finalParentId);
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库查询完成，开始处理查询结果");
            LogUtils.info(logger, () -> "【菜单管理】根据父菜单ID " + finalParentId + " 查询子菜单成功，共 " + menus.size() + " 条记录");
            LogUtils.logBusiness(logger, "MENUS_BY_PARENT_ID_QUERY_SUCCESS", "system", "根据父菜单ID " + finalParentId + " 查询子菜单成功，记录数: " + menus.size());
            
            LogUtils.logPerformance(logger, "getMenusByParentId", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】getMenusByParentId方法执行完成，返回子菜单列表");
            return menus;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】根据父菜单ID查询子菜单异常: " + parentId + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENUS_BY_PARENT_ID_QUERY_FAILED", "system", "根据父菜单ID查询子菜单失败: " + parentId + ", " + e.getMessage());
            throw e;
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createMenu(Menu menu) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENU_CREATE_ATTEMPT", "system", "创建菜单尝试开始");
        try {
            // 参数验证
            if (menu == null) {
                LogUtils.warn(logger, "【菜单管理】createMenu方法参数验证失败: 菜单对象为空");
                LogUtils.logBusiness(logger, "MENU_CREATE_FAILED", "system", "创建菜单参数验证失败: 菜单对象为空");
                return false;
            }
            if (menu.getTitle() == null || menu.getTitle().trim().isEmpty()) {
                LogUtils.warn(logger, "【菜单管理】createMenu方法参数验证失败: 菜单标题为空");
                LogUtils.logBusiness(logger, "MENU_CREATE_FAILED", "system", "创建菜单参数验证失败: 菜单标题为空");
                return false;
            }
            
            LogUtils.logBusiness(logger, "MENU_CREATE_PROCESS", "system", "创建菜单处理开始: " + menu.getTitle());
            LogUtils.debug(logger, () -> "【菜单管理】开始执行createMenu方法创建菜单: " + menu.getTitle());
            LogUtils.debug(logger, () -> "【菜单管理】createMenu方法执行开始时间戳: " + startTime);
            
            // 设置默认值
            LogUtils.debug(logger, () -> "【菜单管理】开始设置菜单默认值");
            if (menu.getParentId() == null) {
                menu.setParentId(0L);
                LogUtils.debug(logger, () -> "【菜单管理】父菜单ID为空，设置为默认值: 0");
            }
            if (menu.getHidden() == null) {
                menu.setHidden(0);
                LogUtils.debug(logger, () -> "【菜单管理】隐藏状态为空，设置为默认值: 0");
            }
            if (menu.getSort() == null) {
                menu.setSort(0);
                LogUtils.debug(logger, () -> "【菜单管理】排序字段为空，设置为默认值: 0");
            }
            if (menu.getStatus() == null) {
                menu.setStatus(1);
                LogUtils.debug(logger, () -> "【菜单管理】状态为空，设置为默认值: 1");
            }
            
            // 设置时间
            LogUtils.debug(logger, () -> "【菜单管理】开始设置菜单时间戳");
            menu.setCreateTime(LocalDateTime.now());
            menu.setUpdateTime(LocalDateTime.now());
            LogUtils.debug(logger, () -> "【菜单管理】菜单信息准备完成，标题: " + menu.getTitle() + ", 父ID: " + menu.getParentId() + ", 路径: " + menu.getPath());
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.insert方法插入数据库");
            boolean result = menuMapper.insert(menu) > 0;
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库插入完成，开始处理结果");
            if (result) {
                LogUtils.info(logger, () -> "【菜单管理】菜单创建成功: " + menu.getTitle() + " (ID: " + menu.getId() + ")");
                LogUtils.logBusiness(logger, "MENU_CREATE_SUCCESS", "system", "菜单创建成功: " + menu.getTitle() + " (ID: " + menu.getId() + ")");
            } else {
                LogUtils.warn(logger, "【菜单管理】菜单创建失败: {}", menu.getTitle());
                LogUtils.logBusiness(logger, "MENU_CREATE_FAILED", "system", "菜单创建失败: " + menu.getTitle());
            }
            
            LogUtils.logPerformance(logger, "createMenu", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】createMenu方法执行完成，结果: " + result);
            return result;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】创建菜单异常: " + menu.getTitle() + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENU_CREATE_ERROR", "system", "创建菜单发生异常: " + menu.getTitle() + ", " + e.getMessage());
            throw e;
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateMenu(Menu menu) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENU_UPDATE_ATTEMPT", "system", "更新菜单尝试开始");
        try {
            // 参数验证
            if (menu == null) {
                LogUtils.warn(logger, "【菜单管理】updateMenu方法参数验证失败: 菜单对象为空");
                LogUtils.logBusiness(logger, "MENU_UPDATE_FAILED", "system", "更新菜单参数验证失败: 菜单对象为空");
                return false;
            }
            if (menu.getId() == null || menu.getId() <= 0) {
                LogUtils.warn(logger, "【菜单管理】updateMenu方法参数验证失败: 菜单ID为空或非法");
                LogUtils.logBusiness(logger, "MENU_UPDATE_FAILED", "system", "更新菜单参数验证失败: 菜单ID为空或非法");
                return false;
            }
            
            // 验证菜单是否存在
            LogUtils.debug(logger, () -> "【菜单管理】验证菜单是否存在，ID: " + menu.getId());
            Menu existingMenu = menuMapper.findById(menu.getId());
            if (existingMenu == null) {
                LogUtils.warn(logger, "【菜单管理】更新菜单失败: ID={}, 原因: 菜单不存在", menu.getId());
                LogUtils.logBusiness(logger, "MENU_UPDATE_FAILED", "system", "更新菜单失败: ID=" + menu.getId() + "，菜单不存在");
                return false;
            }
            
            LogUtils.logBusiness(logger, "MENU_UPDATE_PROCESS", "system", "更新菜单处理开始: " + menu.getId() + " - " + menu.getTitle());
            LogUtils.debug(logger, () -> "【菜单管理】开始执行updateMenu方法更新菜单: " + menu.getId() + " - " + menu.getTitle());
            LogUtils.debug(logger, () -> "【菜单管理】updateMenu方法执行开始时间戳: " + startTime);
            
            LogUtils.debug(logger, () -> "【菜单管理】设置菜单更新时间戳");
            menu.setUpdateTime(LocalDateTime.now());
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.update方法更新数据库");
            boolean result = menuMapper.update(menu) > 0;
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库更新完成，开始处理结果");
            if (result) {
                LogUtils.info(logger, () -> "【菜单管理】菜单更新成功: " + menu.getId() + " - " + menu.getTitle());
                LogUtils.logBusiness(logger, "MENU_UPDATE_SUCCESS", "system", "菜单更新成功: " + menu.getId() + " - " + menu.getTitle());
            } else {
                LogUtils.warn(logger, "【菜单管理】菜单更新失败: {} - {}", menu.getId(), menu.getTitle());
                LogUtils.logBusiness(logger, "MENU_UPDATE_FAILED", "system", "菜单更新失败: " + menu.getId() + " - " + menu.getTitle());
            }
            
            LogUtils.logPerformance(logger, "updateMenu", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】updateMenu方法执行完成，结果: " + result);
            return result;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】更新菜单异常: " + menu.getId() + " - " + menu.getTitle() + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENU_UPDATE_ERROR", "system", "更新菜单发生异常: " + menu.getId() + " - " + menu.getTitle() + ", " + e.getMessage());
            throw e;
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteMenu(Long id) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENU_DELETE_ATTEMPT", "system", "删除菜单尝试开始");
        try {
            // 参数验证
            if (id == null || id <= 0) {
                LogUtils.warn(logger, "【菜单管理】deleteMenu方法参数验证失败: ID为空或非法值: " + id);
                LogUtils.logBusiness(logger, "MENU_DELETE_FAILED", "system", "删除菜单参数验证失败: " + id);
                return false;
            }
            
            // 验证菜单是否存在
            LogUtils.debug(logger, () -> "【菜单管理】验证菜单是否存在，ID: " + id);
            Menu existingMenu = menuMapper.findById(id);
            if (existingMenu == null) {
                LogUtils.warn(logger, "【菜单管理】删除菜单失败: ID={}, 原因: 菜单不存在", id);
                LogUtils.logBusiness(logger, "MENU_DELETE_FAILED", "system", "删除菜单失败: ID=" + id + "，菜单不存在");
                return false;
            }
            
            LogUtils.logBusiness(logger, "MENU_DELETE_PROCESS", "system", "删除菜单处理开始: " + id + " - " + existingMenu.getTitle());
            LogUtils.debug(logger, () -> "【菜单管理】开始执行deleteMenu方法删除菜单: " + id + " - " + existingMenu.getTitle());
            LogUtils.debug(logger, () -> "【菜单管理】deleteMenu方法执行开始时间戳: " + startTime);
            
            // 先检查是否有子菜单
            LogUtils.debug(logger, () -> "【菜单管理】检查菜单是否有子菜单: " + id);
            List<Menu> subMenus = menuMapper.findByParentId(id);
            if (subMenus != null && !subMenus.isEmpty()) {
                LogUtils.warn(logger, "【菜单管理】菜单删除失败: ID={}, 原因: 存在子菜单，数量={}", id, subMenus.size());
                LogUtils.logBusiness(logger, "MENU_DELETE_FAILED", "system", "删除菜单失败: ID=" + id + "，存在" + subMenus.size() + "个子菜单");
                return false; // 存在子菜单，不允许删除
            }
            
            LogUtils.debug(logger, () -> "【菜单管理】菜单无子菜单，可以删除");
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.delete方法删除数据库记录");
            boolean result = menuMapper.delete(id) > 0;
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库删除完成，开始处理结果");
            if (result) {
                LogUtils.info(logger, () -> "【菜单管理】菜单删除成功: " + id + " - " + existingMenu.getTitle());
                LogUtils.logBusiness(logger, "MENU_DELETE_SUCCESS", "system", "菜单删除成功: " + id + " - " + existingMenu.getTitle());
            } else {
                LogUtils.warn(logger, "【菜单管理】菜单删除失败: ID={}, 原因: 菜单不存在或已被删除", id);
                LogUtils.logBusiness(logger, "MENU_DELETE_FAILED", "system", "菜单删除失败: ID=" + id + "，删除操作未成功");
            }
            
            LogUtils.logPerformance(logger, "deleteMenu", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】deleteMenu方法执行完成，结果: " + result);
            return result;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】删除菜单异常: " + id + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENU_DELETE_ERROR", "system", "删除菜单发生异常: " + id + ", " + e.getMessage());
            throw e;
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateMenuStatus(Long id, Integer status) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_ATTEMPT", "system", "更新菜单状态尝试开始");
        try {
            // 参数验证
            if (id == null || id <= 0) {
                LogUtils.warn(logger, "【菜单管理】updateMenuStatus方法参数验证失败: ID为空或非法值: " + id);
                LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_FAILED", "system", "更新菜单状态参数验证失败: ID非法");
                return false;
            }
            if (status == null || (status != 0 && status != 1)) {
                LogUtils.warn(logger, "【菜单管理】updateMenuStatus方法参数验证失败: 状态值非法: " + status);
                LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_FAILED", "system", "更新菜单状态参数验证失败: 状态值非法");
                return false;
            }
            
            // 验证菜单是否存在
            LogUtils.debug(logger, () -> "【菜单管理】验证菜单是否存在，ID: " + id);
            Menu existingMenu = menuMapper.findById(id);
            if (existingMenu == null) {
                LogUtils.warn(logger, "【菜单管理】更新菜单状态失败: ID={}, 原因: 菜单不存在", id);
                LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_FAILED", "system", "更新菜单状态失败: ID=" + id + "，菜单不存在");
                return false;
            }
            
            // 检查状态是否相同
            if (existingMenu.getStatus() != null && existingMenu.getStatus().equals(status)) {
                LogUtils.debug(logger, () -> "【菜单管理】菜单状态无需更新，当前状态已为: " + status + "，菜单ID: " + id);
                LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_NO_CHANGE", "system", "菜单状态无需更新: ID=" + id + "，状态已为" + status);
                return true; // 状态相同，无需更新
            }
            
            LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_PROCESS", "system", "更新菜单状态处理开始: " + id + ", 新状态: " + status);
            LogUtils.debug(logger, () -> "【菜单管理】开始执行updateMenuStatus方法更新菜单状态: " + id + ", 状态: " + status);
            LogUtils.debug(logger, () -> "【菜单管理】updateMenuStatus方法执行开始时间戳: " + startTime);
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.updateStatus方法更新数据库状态");
            boolean result = menuMapper.updateStatus(id, status) > 0;
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库状态更新完成，开始处理结果");
            if (result) {
                LogUtils.info(logger, () -> "【菜单管理】菜单状态更新成功: " + id + " - " + existingMenu.getTitle() + ", 新状态: " + status);
                LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_SUCCESS", "system", "菜单状态更新成功: " + id + ", 新状态: " + status);
            } else {
                LogUtils.warn(logger, "【菜单管理】菜单状态更新失败: ID={}, 状态={}, 原因: 菜单不存在或更新失败", id, status);
                LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_FAILED", "system", "菜单状态更新失败: ID=" + id + ", 状态=" + status);
            }
            
            LogUtils.logPerformance(logger, "updateMenuStatus", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】updateMenuStatus方法执行完成，结果: " + result);
            return result;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】更新菜单状态异常: " + id + ", 状态: " + status + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENU_STATUS_UPDATE_ERROR", "system", "更新菜单状态发生异常: " + id + ", " + e.getMessage());
            throw e;
        }
    }
    
    @Override
    public List<Map<String, Object>> getMenuTree(Long parentId) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENU_TREE_QUERY_START", "system", "准备获取菜单树数据，父ID: " + parentId);
        try {
            LogUtils.debug(logger, () -> "【菜单管理】开始执行getMenuTree方法获取菜单树");
            LogUtils.debug(logger, () -> "【菜单管理】getMenuTree方法执行开始时间戳: " + startTime);
            
            // 创建final本地变量保存可能修改后的值
            LogUtils.debug(logger, () -> "【菜单管理】处理父菜单ID参数");
            final Long finalParentId;
            if (parentId == null) {
                finalParentId = 0L;
                LogUtils.debug(logger, () -> "【菜单管理】父菜单ID为空，设置为默认值: 0");
                LogUtils.logBusiness(logger, "MENU_TREE_QUERY_DEFAULT_PARENT", "system", "父菜单ID为空，使用默认值0");
            } else {
                finalParentId = parentId;
                LogUtils.debug(logger, () -> "【菜单管理】使用指定的父菜单ID: " + finalParentId);
            }
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.getMenuTree方法查询数据库菜单树，父菜单ID: " + finalParentId);
            List<Map<String, Object>> menuTree = menuMapper.getMenuTree(finalParentId);
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库查询完成，开始处理查询结果");
            LogUtils.info(logger, () -> "【菜单管理】获取菜单树成功，共 " + menuTree.size() + " 个节点");
            LogUtils.logBusiness(logger, "MENU_TREE_QUERY_SUCCESS", "system", "获取菜单树成功，节点数: " + menuTree.size());
            
            LogUtils.logPerformance(logger, "getMenuTree", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】getMenuTree方法执行完成，返回菜单树数据");
            return menuTree;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】获取菜单树异常: " + parentId + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENU_TREE_QUERY_FAILED", "system", "获取菜单树失败: " + parentId + ", " + e.getMessage());
            throw e;
        }
    }
    
    @Override
    public List<Menu> getMenusByRoleId(Long roleId) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "MENUS_BY_ROLE_ID_QUERY_START", "system", "准备根据角色ID查询菜单，角色ID: " + roleId);
        try {
            // 参数验证
            if (roleId == null || roleId <= 0) {
                LogUtils.warn(logger, "【菜单管理】getMenusByRoleId方法参数验证失败: 角色ID为空或非法值: " + roleId);
                LogUtils.logBusiness(logger, "MENUS_BY_ROLE_ID_QUERY_FAILED", "system", "根据角色ID查询菜单参数验证失败: " + roleId);
                return null;
            }
            
            LogUtils.debug(logger, () -> "【菜单管理】开始执行getMenusByRoleId方法，角色ID: " + roleId);
            LogUtils.debug(logger, () -> "【菜单管理】getMenusByRoleId方法执行开始时间戳: " + startTime);
            
            LogUtils.debug(logger, () -> "【菜单管理】调用menuMapper.findByRoleId方法查询数据库，角色ID: " + roleId);
            List<Menu> menus = menuMapper.findByRoleId(roleId);
            
            LogUtils.debug(logger, () -> "【菜单管理】数据库查询完成，开始处理查询结果");
            LogUtils.info(logger, () -> "【菜单管理】根据角色ID " + roleId + " 查询菜单成功，共 " + menus.size() + " 条记录");
            LogUtils.logBusiness(logger, "MENUS_BY_ROLE_ID_QUERY_SUCCESS", "system", "根据角色ID " + roleId + " 查询菜单成功，记录数: " + menus.size());
            
            LogUtils.logPerformance(logger, "getMenusByRoleId", startTime, System.currentTimeMillis());
            LogUtils.debug(logger, () -> "【菜单管理】getMenusByRoleId方法执行完成，返回角色菜单列表");
            return menus;
        } catch (Exception e) {
            LogUtils.error(logger, "【菜单管理】根据角色ID查询菜单异常: " + roleId + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logBusiness(logger, "MENUS_BY_ROLE_ID_QUERY_FAILED", "system", "根据角色ID查询菜单失败: " + roleId + ", " + e.getMessage());
            throw e;
        }
    }
}